/**
 * @author monirul
 */

var UserHistoryView =  new Class.create({
	
	initialize: function(){
	
	},
	
	init:function(o){
		if(o['componentId'] != null) this._componentId = o['componentId'];
		if(o['model'] != null) this._model = o['model'];
		if(o['controller'] != null) this._controller = o['controller'];
		if(o['elements'] != null){
			this._elements = o['elements'];
			this._targetLocation = this._elements['targetLocation'];
		}	
		 var _this = this;
		// attach model listeners
	    this._model.userHistoryUpdated.attach(function () {
	        _this.rebuildList();
	    });
//	    this._model.displayUserHistory.attach(function () {
//	        _this.showHistoryWindow();
//	    });
	   
	},
	show : function () {
        this.rebuildList();
	},
	
	
	rebuildList: function() {
		//var myPanel =  new YAHOO.widget.Panel("panel1", { width:"320px", visible:true, constraintoviewport:true,underlay: "matte"  } );
		
		var myPanel1 =  new YAHOO.widget.Panel("historyPanel", { width:"720px", visible:true, constraintoviewport:true,underlay: "matte" ,fixedcenter: true } );
		myPanel1.setHeader("History");
		//myPanel.render();
		//return;
		//window.open('/historyWindow.html','History','width=300,height=300,scrollbars=yes');
		this.usersHistoryData = new Array();
//		this.height = 0;
		var userHistory = this._model.getUserHistory();
		for(var i=0;i<userHistory.length; i++){
				this.detailBtnDisabled = " DISABLED ";
				if(userHistory[i].userGameHistoryList != null && userHistory[i].userGameHistoryList.length>0){
					this.detailBtnDisabled = "  ";
				}
				this.detailLink = '<input type="button"  onclick="alert(\'deail history\');componentExecFunctionWithCall(\''+this._componentId+'\',\'displayGameHistory\',\''+userHistory[i].userHistoryId+'\');"  value="detail"'+ this.detailBtnDisabled+'/>';
//////			this.userLink = '<a  onclick="alert(\'user\');this.showHistoryWindow();" >'+userList[i].name+'</a>'
				this.usersHistoryData[i] = {
											'loginTime':userHistory[i].loginTime ,
											'logoutTime':userHistory[i].logoutTime,
											'gamePlayed':userHistory[i].noOfGamePlayed,
											'detailHistory':this.detailLink 
											}
		}
		this.columnDefs = [ 
				            {key:"loginTime", label:"Login Time", sortable:true, resizeable:true,width:195}, 
				            {key:"logoutTime",label:"Logout Time", sortable:true, resizeable:true,width:153},
				            {key:"gamePlayed",label:"#of Game", sortable:true, resizeable:true,width:153},
				            {key:"detailHistory",label:" # ", sortable:true, resizeable:true,width:153}
				            //, 
				            //{key:"owner",lebel:"Room Owner", resizeable:true}
				        ]; 
		this.rowFormatter = function(elTr, oRecord) { 
//			if (oRecord.getData('roomName').length>0 ) { 
//				YAHOO.util.Dom.addClass(elTr, 'mark'); 
//			} 
			return true; 
		}; 

		var init = {'colDef':this.columnDefs,'title':"User History",'targetPosition':this._targetLocation};
		var userListTbl = ComponentManager.loadComponent({componentId:'userHistory_table',
			jsClassName:'DataTableWidget',initData:init});
//		userListTbl.setRowFormatter(this.rowFormatter);
		userListTbl.drawTable(this.usersHistoryData);
		myPanel1.render();
	},
	
	
	displayGameHistory:function(userHistoryId){
//		alert('displayGameHistory');
		var myPanel1 =  new YAHOO.widget.Panel("historyPanel", { width:"720px", visible:true, constraintoviewport:true,underlay: "matte" ,fixedcenter: true } );
		myPanel1.setHeader("History");
		
		this.roomsHistoryData = new Array();
		var gameUserHistoryList = this._model.getUserGameHistory(userHistoryId);
		if(gameUserHistoryList == null) return;
		for(var i=0;i<gameUserHistoryList.length; i++){
				var userStatus = "PLAYER"; 
				if(gameUserHistoryList[i].userResultStatus == 2 ){
					userStatus = "DEAD";
				}else if(gameUserHistoryList[i].userResultStatus == 6 ){
					userStatus = "WINNER";
				}
				var gameStatus = "INITIALIZED"; 
					if(gameUserHistoryList[i].gameStatus == 1 ){
						gameStatus = "NOT COMPLETED";
					}else if(gameUserHistoryList[i].gameStatus == 3 ){
						gameStatus = "GAME OVER";
					}
					
				this.roomsHistoryData[i] = {
											'roomName':gameUserHistoryList[i].roomName ,
											'userStatus':userStatus,
											'gameStatus':gameStatus,
											'role':gameUserHistoryList[i].roleName,
											'character':gameUserHistoryList[i].characterName,
											'startTime':gameUserHistoryList[i].startTime,
											'endTime':gameUserHistoryList[i].endTime
											}
		}
//		
		this.columnDefs = [ 
				            {key:"roomName", label:"Room name", sortable:true, resizeable:true,width:195}, 
				            {key:"userStatus",label:"User Status", sortable:true, resizeable:true,width:153},
				            {key:"gameStatus",label:"Game Status", sortable:true, resizeable:true,width:153},
				            {key:"role",label:" Role", sortable:true, resizeable:true,width:153},
				            {key:"character",label:"Character ", sortable:true, resizeable:true,width:153},
				            {key:"startTime",label:"Start Time ", sortable:true, resizeable:true,width:153},
				            {key:"endTime",label:"End Time ", sortable:true, resizeable:true,width:153}
				            
				            //, 
				            //{key:"owner",lebel:"Room Owner", resizeable:true}
				        ]; 
		this.rowFormatter = function(elTr, oRecord) { 
//			if (oRecord.getData('roomName').length>0 ) { 
//				YAHOO.util.Dom.addClass(elTr, 'mark'); 
//			} 
			return true; 
		}; 

		var init = {'colDef':this.columnDefs,'title':"Game History",'targetPosition':this._targetLocation};
//		var roomListTbl = ComponentManager.loadComponent({componentId:'gameUserHistoryList_table',
//			jsClassName:'DataTableWidget',initData:init});
//		roomListTbl.setRowFormatter(this.rowFormatter);
		//roomListTbl.drawTable(this.roomsHistoryData);
		myPanel1.render();
	}
	
	
	
});
